package com.aruba.springsecuritystudy.security.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;


@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 模拟数据库查询操作
        if (!username.equals("aruba")) throw new UsernameNotFoundException("用户未找到");
        // 查询出的密码
        String pwd = "123";

        // 密码解析器
        String encodePassword = passwordEncoder.encode(pwd);
        User user = new User(username, encodePassword,
                AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_admin,modify"));

        return user;
    }
}
